import unittest
from alg import Solution
from gen import generate_test_case

class TestSubsetXORSum(unittest.TestCase):
    def test_empty_array(self):
        self.assertEqual(Solution().subsetXORSum([]), 0)

    def test_single_element(self):
        self.assertEqual(Solution().subsetXORSum([5]), 5)

    def test_two_elements(self):
        self.assertEqual(Solution().subsetXORSum([1, 2]), 6)  # 解释见解题报告

    def test_sample_case(self):
        self.assertEqual(Solution().subsetXORSum([3, 4, 5]), 28)

    def test_random_cases(self):
        # 测试10个随机生成的用例
        for _ in range(10):
            nums = generate_test_case()
            # 使用暴力法验证
            expected = self._brute_force_xor_sum(nums)
            self.assertEqual(Solution().subsetXORSum(nums), expected)

    def _brute_force_xor_sum(self, nums):
        """暴力枚举所有子集的异或和，用于测试验证"""
        from itertools import combinations
        total = 0
        for r in range(len(nums) + 1):
            for subset in combinations(nums, r):
                xor = 0
                for num in subset:
                    xor ^= num
                total += xor
        return total

if __name__ == "__main__":
    unittest.main()    